#!/usr/bin/python

import sys, getopt, logging, devio, config, time
from owhandler import OWHandler
from device import Switch 
from owfs import Owfs 

test_switch = "test_switch"

owfs = Owfs()


def func_name():
    import traceback
    return traceback.extract_stack(None, 2)[0][2]

def log(msg):
    print "\n" + msg + "\n"

def setAlarm(addr, byte):
    pass

def result(funcname, success):
    log("*** %s %s ***" % (funcname, "OK" if success else "ERROR"))
    return success

def test1():    
    log("fast switch press & release")
   
    owh = OWHandler("./testcfg/test1.conf")
    owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
    owfs.write(test_switch + "/sensed.BYTE", str(15))
    out = int(owfs.read(test_switch + "/PIO.7"))
    owh.handleAlarms()
    owh.handleTimers()

    out2 = int(owfs.read(test_switch + "/PIO.7"))
    return result(func_name(), out2 != out)

def test1a():
    log("twice fast switch press & release")
   
    owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
    owfs.write(test_switch + "/sensed.BYTE", str(15))
    out = int(owfs.read(test_switch + "/PIO.7"))
    owh = OWHandler("./testcfg/test1.conf")

    owh.handleAlarms()
    owh.handleTimers()
    owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
    owh.handleAlarms()
    owh.handleTimers()

    out2 = int(owfs.read(test_switch + "/PIO.7"))
    return result(func_name(), out2 != out)

def test1b():
    log("twice slow switch press & release")
   
    owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
    owfs.write(test_switch + "/sensed.BYTE", str(15))
    out = int(owfs.read(test_switch + "/PIO.7"))
    owh = OWHandler("./testcfg/test1.conf")

    owh.handleAlarms()
    owh.handleTimers()
    time.sleep(0.6)
    owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
    owh.handleAlarms()
    owh.handleTimers()

    out2 = int(owfs.read(test_switch + "/PIO.7"))
    return result(func_name(), out2 == out)

def test1c():
    log("10x fast switch press & release")
   
    owfs.write(test_switch + "/sensed.BYTE", str(15))
    out = int(owfs.read(test_switch + "/PIO.7"))
    owh = OWHandler("./testcfg/test1.conf")

    for i in range(10):
        owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
        owh.handleAlarms()
        owh.handleTimers()
        out2 = int(owfs.read(test_switch + "/PIO.7"))
        if out2 == out:
            return result(func_name(), out2 != out)
    return result(func_name(), out2 != out)

def test1d():
    log("10x slow switch press & release")
   
    owfs.write(test_switch + "/sensed.BYTE", str(15))
    owfs.write(test_switch + "/PIO.7", "0")
    owh = OWHandler("./testcfg/test1.conf")

    for i in range(10):
        owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
        owh.handleAlarms()
        owh.handleTimers()
        out2 = int(owfs.read(test_switch + "/PIO.7"))
        time.sleep(0.6)
        print out2
        if not result(func_name(), out2 == 1):
            return False
        owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
        owh.handleAlarms()
        owh.handleTimers()
        out2 = int(owfs.read(test_switch + "/PIO.7"))
        time.sleep(0.6)
        print out2
        if not result(func_name(), out2 == 0):
            return False
    return True

def test1e():
    log("slowly press & release switch")
   
    owh = OWHandler("./testcfg/test1.conf")

    owfs.write(test_switch + "/PIO.7", "0")
    owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
    owfs.write(test_switch + "/sensed.BYTE", str(~4))
    owh.handleAlarms()
    owh.handleTimers()
    out = int(owfs.read(test_switch + "/PIO.7"))
    if not result(func_name(), out == 1):
        return False

    time.sleep(0.5)
    owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
    owfs.write(test_switch + "/sensed.BYTE", str(~0))
    owh.handleAlarms()
    owh.handleTimers()

    out = int(owfs.read(test_switch + "/PIO.7"))
    return result(func_name(), out == 1)

def test1f():
    log("slowly press & release switch, play empty scene")
   
    owh = OWHandler("./testcfg/test1f.conf")

    owfs.write(test_switch + "/PIO.7", "0")
    owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
    owfs.write(test_switch + "/sensed.BYTE", str(~4))
    owh.handleAlarms()
    owh.handleTimers()
    out = int(owfs.read(test_switch + "/PIO.7"))
    if not result(func_name(), out == 0):   # if scene, switch shall toggle on falling edge
        return False

    time.sleep(Switch._holdTime + 0.1)
    owh.handleAlarms()
    owh.handleTimers()
    if not result(func_name(), out == 0):
        return False

    time.sleep(Switch._holdTime + 0.1)
    owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
    owfs.write(test_switch + "/sensed.BYTE", str(~0))
    owh.handleAlarms()
    owh.handleTimers()

    out = int(owfs.read(test_switch + "/PIO.7"))
    return result(func_name(), out == 0)

def test2():
    log("switch on monostable w/o sustain")
    owh = OWHandler("./testcfg/test2.conf")

    owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
    owfs.write(test_switch + "/sensed.BYTE", str(15))
    owfs.write(test_switch + "/PIO.7", "0")

    owh.handleAlarms()
    owh.handleTimers()

    out = int(owfs.read(test_switch + "/PIO.7"))
    return result(func_name(), out == 1)

def test2a():
    log("2x fast switch on monostable w/o sustain")
    owh = OWHandler("./testcfg/test2.conf")

    owfs.write(test_switch + "/sensed.BYTE", str(15))
    owfs.write(test_switch + "/PIO.7", "0")

    owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
    owh.handleAlarms()
    owh.handleTimers()
    owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
    owh.handleAlarms()
    owh.handleTimers()


    out = int(owfs.read(test_switch + "/PIO.7"))
    return result(func_name(), out == 1)

def test2b():
    log("fast & slow switch on monostable w/o sustain")
    owh = OWHandler("./testcfg/test2.conf")

    owfs.write(test_switch + "/sensed.BYTE", str(15))

    owfs.write(test_switch + "/PIO.7", "0")
    owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
    owh.handleAlarms()
    owh.handleTimers()
    out = int(owfs.read(test_switch + "/PIO.7"))
    if not result(func_name(), out == 1):
        return False

    time.sleep(Switch._idleTime - 0.1)

    owfs.write(test_switch + "/PIO.7", "0")
    owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
    owh.handleAlarms()
    owh.handleTimers()
    out = int(owfs.read(test_switch + "/PIO.7"))
    if not result(func_name(), out == 0):
        return False

    time.sleep(Switch._idleTime + 0.1)

    owfs.write(test_switch + "/PIO.7", "0")
    owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
    owh.handleAlarms()
    owh.handleTimers()
    out = int(owfs.read(test_switch + "/PIO.7"))

    return result(func_name(), out == 1)

def test3():
    log("switch on monostable w. sustain 2s")
    owh = OWHandler("./testcfg/test3.conf")

    owfs.write(test_switch + "/sensed.BYTE", str(15))

    owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
    owfs.write(test_switch + "/PIO.7", "0")

    owh.handleAlarms()
    owh.handleTimers()
    out = int(owfs.read(test_switch + "/PIO.7"))
    if not result(func_name(), out == 1):
        return False

    time.sleep(1)
    owh.handleAlarms()
    owh.handleTimers()
    out = int(owfs.read(test_switch + "/PIO.7"))
    if not result(func_name(), out == 1):
        return False

    time.sleep(1.1)
    owh.handleAlarms()
    owh.handleTimers()
    out = int(owfs.read(test_switch + "/PIO.7"))
    return result(func_name(), out == 0)

def test3a():
    log("switch on monostable w. sustain 2s, activate again during sustain")
    owh = OWHandler("./testcfg/test3.conf")

    owfs.write(test_switch + "/sensed.BYTE", str(15))

    owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
    owfs.write(test_switch + "/PIO.7", "0")

    owh.handleAlarms()
    owh.handleTimers()
    out = int(owfs.read(test_switch + "/PIO.7"))
    if not result(func_name(), out == 1):
        return False

    time.sleep(1)
    owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
    owh.handleAlarms()
    owh.handleTimers()
    out = int(owfs.read(test_switch + "/PIO.7"))
    if not result(func_name(), out == 1):
        return False

    time.sleep(1)
    owh.handleAlarms()
    owh.handleTimers()
    out = int(owfs.read(test_switch + "/PIO.7"))
    if not result(func_name(), out == 1):
        return False

    time.sleep(1.1)
    owh.handleAlarms()
    owh.handleTimers()
    out = int(owfs.read(test_switch + "/PIO.7"))
    return result(func_name(), out == 0)

def test4():
    log("filter disturbances")

    owfs.write(test_switch + "/sensed.BYTE", str(15))
    owfs.write(test_switch + "/PIO.2", "0")
    owh = OWHandler("./testcfg/test1.conf")
    
    for i in range(11):
        time.sleep(0.1)
        owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
        owh.df.checkAll()
        owh.handleAlarms()
        owh.handleTimers()
        pio2 = owfs.read(test_switch + "/PIO.2")
        alm = owfs.read(test_switch + "/set_alarm")
        print pio2
        if i < 10:
            if not result(func_name(), pio2 == "0" and alm == "133333333"):
                return False
        else:
            if not result(func_name(), pio2 == "1" and alm == "133333333"):
                return False

    for i in range(11):
        time.sleep(0.1)
        owfs.write("/alarm/" + test_switch + "/latch.BYTE", "4")
        owh.df.checkAll()
        owh.handleAlarms()
        owh.handleTimers()
        alm = owfs.read(test_switch + "/set_alarm")
        print alm
        if i < 10:
            if not result(func_name(), alm == "133333333"):
                return False
        else:
            return result(func_name(), alm == "133333033")

    return True



def test666():
    "real switch toggle"
    owh = OWHandler("./testcfg/test666.conf")
    #owh.config.switches["wiatrolap_kin.Sw1"].redir.on().delay(1).off().delay(1).on().delay(1).off()
    #owh.config.switches["gabinet_lampa_test.Sw1"].redir.on().delay(1).off().delay(1).on().delay(1).off()
    #owh.config.switches["wiatrolap_sw.Sw1"].redir.on().delay(1).off().delay(1).on().delay(1).off()
    #owh.config.switches["lazienkad_lampa_pir.Pir"].redir.on().delay(1).off().delay(1).on().delay(1).off()
    return True


def main():
    logging.basicConfig(level=logging.DEBUG)

    test4()
    return

    for i in range(10):
        if not test1(): break
        if not test1a(): break
        if not test1b(): break
        if not test1c(): break
        if not test1d(): break
        if not test1e(): break
        if not test1f(): break
    
        if not test2(): break
        if not test2a(): break
        if not test2b(): break
    
        if not test3(): break
        if not test3a(): break

        if not test4(): break

main()
